package design.mode.model.composite;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 菜单类：属于 树枝节点
 * @since jdk11
 */
public class Menu extends MenuComponent {

    //聚合MenuComponent：菜单有一到多个子菜单
    private List<MenuComponent> menuComponentList = new ArrayList<>();

    public Menu(Builder builder) {
        this.id = builder.menuId;
        this.parentId = builder.menuParentId;
        this.name = builder.menuName;
        this.level = builder.menuLevel;
        this.url = builder.menuUrl;
        this.effective = builder.menuEffective;
    }

    @Override
    public void add(MenuComponent menuComponent) {
        menuComponentList.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        menuComponentList.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int index) {
        return menuComponentList.get(index);
    }

    @Override
    public void print() {
        for (MenuComponent component : menuComponentList) {
            if("0".equals(component.parentId)){
                System.out.println(component);
            }
        }
        //打印菜单
        Map<String,Object> tree = new HashMap<>();
        tree.put("tree",menuComponentList);
        System.out.println(tree);
    }

    public static final   class Builder {
        protected String menuId;
        protected String menuParentId;
        protected String menuUrl;
        protected boolean menuEffective;
        protected String menuName;
        protected int menuLevel;

        public Builder id(String menuId) {
            this.menuId = menuId;
            return this;
        }

        public Builder parentId(String parentId) {
            this.menuParentId = parentId;
            return this;
        }

        public Builder url(String menuUrl) {
            this.menuUrl = menuUrl;
            return this;
        }

        public Builder menuName(String menuName) {
            this.menuName = menuName;
            return this;
        }

        public Builder effective(boolean menuEffective) {
            this.menuEffective = menuEffective;
            return this;
        }

        public Builder menuLevel(int menuLevel) {
            this.menuLevel = menuLevel;
            return this;
        }

        public Menu build() {
            return new Menu(this);
        }
    }

    @Override
    public String toString() {
        return "Menu{" +
                "menuComponentList=" + menuComponentList +
                ", id='" + id + '\'' +
                ", parentId='" + parentId + '\'' +
                ", url='" + url + '\'' +
                ", effective=" + effective +
                ", name='" + name + '\'' +
                ", level=" + level +
                '}';
    }
}
